/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package com.cn.xiaonuo.tcrfinance.sysmgmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cn.xiaonuo.core.factory.PageFactory;
import com.cn.xiaonuo.core.pojo.page.PageResult;
import com.cn.xiaonuo.tcrfinance.sysmgmt.entity.SysConfigEntity;
import com.cn.xiaonuo.tcrfinance.sysmgmt.mapper.SysConfigHpcMapper;
import com.cn.xiaonuo.tcrfinance.sysmgmt.param.SysConfigHpcParam;
import com.cn.xiaonuo.tcrfinance.sysmgmt.service.SysConfigHpcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

@Service
public class SysConfigHpcServiceImpl extends ServiceImpl<SysConfigHpcMapper, SysConfigEntity> implements SysConfigHpcService {
	@Autowired
	private SysConfigHpcMapper sysConfigMapper;
	//@Autowired
	//private SysConfigRedis sysConfigRedis;
	@Override
	public PageResult queryPage(SysConfigHpcParam sysConfigParam) {
		QueryWrapper<SysConfigEntity> queryWrapper = new QueryWrapper<>();
		if (ObjectUtil.isNotNull(sysConfigParam)) {

			// 根据唯一标识 查询
			if (ObjectUtil.isNotEmpty(sysConfigParam.getParamKey())) {
				queryWrapper.lambda().like(SysConfigEntity::getParamKey, sysConfigParam.getParamKey());
			}

		}
		queryWrapper.lambda().eq(SysConfigEntity::getStatus,1);
		return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
	}

	@Override
	public void saveConfig(SysConfigHpcParam sysConfigParam) {
		SysConfigEntity config=new SysConfigEntity();
		BeanUtil.copyProperties(sysConfigParam,config);
		this.save(config);
		//sysConfigRedis.saveOrUpdate(config);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(SysConfigHpcParam sysConfigParam) {
		SysConfigEntity config=new SysConfigEntity();
		BeanUtil.copyProperties(sysConfigParam,config);
		this.updateById(config);
		//sysConfigRedis.saveOrUpdate(config);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateValueByKey(String key, String value) {
		baseMapper.updateValueByKey(key, value);
		//sysConfigRedis.delete(key);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteBatch(Long[] ids) {
		for(Long id : ids){
			SysConfigEntity config = this.getById(id);
			//sysConfigRedis.delete(config.getParamKey());
		}

		this.removeByIds(Arrays.asList(ids));
	}

	/*@Override
	public String getValue(String key) {
		SysConfigEntity config = sysConfigRedis.get(key);
		if(config == null){
			config = baseMapper.queryByKey(key);
			sysConfigRedis.saveOrUpdate(config);
		}

		return config == null ? null : config.getParamValue();
	}

	@Override
	public <T> T getConfigObject(String key, Class<T> clazz) {
		String value = getValue(key);
		if(StringUtils.isNotBlank(value)){
			return new Gson().fromJson(value, clazz);
		}

		try {
			return clazz.newInstance();
		} catch (Exception e) {
			throw new RRException("获取参数失败");
		}
	}*/
}
